using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Threading;
using System.Collections;

namespace ZhiAo.CommonLibrary
{
    public class MailHelper
    {
        public string sSmtpServer;
        public string sUser;
        public string sPassword;

        public MailHelper(string sSmtpServer, string sUser, string sPassword)
        {
            this.sSmtpServer = sSmtpServer;
            this.sPassword = sPassword;
            this.sUser = sUser;
        }

        public void SendMailWithThread(string sFrom, string sTo, string sSubject, string sPriority, string sBody)
        {
            MailThread mailThread = new MailThread(this);
            mailThread.From = sFrom;
            mailThread.To = sTo;
            mailThread.Subject = sSubject;
            mailThread.MailPriority = GetMailPriority(sPriority);
            mailThread.Body = sBody;

            Thread mailWork = new Thread(new ThreadStart(mailThread.SendMail));
            mailWork.IsBackground = true;
            mailWork.Start();
        }

        public void SendMailWithThread(string sFrom, string sTo, string sCc, string sSubject, string sPriority, string sBody)
        {
            MailThread mailThread = new MailThread(this);
            mailThread.From = sFrom;
            mailThread.To = sTo;
            mailThread.Cc = sCc;
            mailThread.Subject = sSubject;
            mailThread.MailPriority = GetMailPriority(sPriority);
            mailThread.Body = sBody;

            Thread mailWork = new Thread(new ThreadStart(mailThread.SendMail));
            mailWork.IsBackground = true;
            mailWork.Start();
        }

        public void SendMailWithThread(string sFrom, string sTo, string sCc, string sBcc, string sSubject, string sPriority, string sBody)
        {
            MailThread mailThread = new MailThread(this);
            mailThread.From = sFrom;
            mailThread.To = sTo;
            mailThread.Cc = sCc;
            mailThread.Bcc = sBcc;
            mailThread.Subject = sSubject;
            mailThread.MailPriority = GetMailPriority(sPriority);
            mailThread.Body = sBody;

            Thread mailWork = new Thread(new ThreadStart(mailThread.SendMail));
            mailWork.IsBackground = true;
            mailWork.Start();
        }

        public void SendMailWithThread(string sFrom, string sTo, string sSubject, string sPriority, string sBody, string sAttachment, int nVirtual)
        {
            MailThread mailThread = new MailThread(this);
            mailThread.From = sFrom;
            mailThread.To = sTo;
            mailThread.Subject = sSubject;
            mailThread.MailPriority = GetMailPriority(sPriority);
            mailThread.Body = sBody;
            mailThread.Attachments = sAttachment;

            Thread mailWork = new Thread(new ThreadStart(mailThread.SendMail));
            mailWork.IsBackground = true;
            mailWork.Start();
        }

        public void SendMail(string sFrom, string sTo, string sSubject, string sPriority, string sBody)
        {
            MailThread mailThread = new MailThread(this);
            mailThread.From = sFrom;
            mailThread.To = sTo;
            mailThread.Subject = sSubject;
            mailThread.MailPriority = GetMailPriority(sPriority);
            mailThread.Body = sBody;
            mailThread.SendMail();
        }

        public void SendMail(string sFrom, string sTo, string sCc, string sSubject, string sPriority, string sBody)
        {
            MailThread mailThread = new MailThread(this);
            mailThread.From = sFrom;
            mailThread.To = sTo;
            mailThread.Cc = sCc;
            mailThread.Subject = sSubject;
            mailThread.MailPriority = GetMailPriority(sPriority);
            mailThread.Body = sBody;
            mailThread.SendMail();
        }

        public void SendMail(string sFrom, string sTo, string sCc, string sBcc, string sSubject, string sPriority, string sBody)
        {
            MailThread mailThread = new MailThread(this);
            mailThread.From = sFrom;
            mailThread.To = sTo;
            mailThread.Cc = sCc;
            mailThread.Bcc = sBcc;
            mailThread.Subject = sSubject;
            mailThread.MailPriority = GetMailPriority(sPriority);
            mailThread.Body = sBody;
            mailThread.SendMail();
        }

        public void SendMail(string sFrom, string sTo, string sSubject, string sPriority, string sBody, string sAttachment, int nVirtual)
        {
            MailThread mailThread = new MailThread(this);
            mailThread.From = sFrom;
            mailThread.To = sTo;
            mailThread.Subject = sSubject;
            mailThread.MailPriority = GetMailPriority(sPriority);
            mailThread.Body = sBody;
            mailThread.Attachments = sAttachment;
            mailThread.SendMail();
        }

        public MailPriority GetMailPriority(string sPriority)
        {
            MailPriority mailPriority = MailPriority.Normal;
            if (sPriority.Equals("High"))
            {
                mailPriority = MailPriority.High;
            }
            else
            {
                if (sPriority.Equals("Low"))
                {
                    mailPriority = MailPriority.Low;
                }
            }

            return mailPriority;

        }
    }

    public class MailThread
    {
        private string sSmtpServer;
        private string sFrom;
        private string sTo;
        private string sCc;
        private string sBcc;
        private string sSubject;
        private MailPriority mailPriority;
        private string sAttachments;
        private string sBody;

        private MailHelper mailHelper;

        public string Attachments
        {
            get { return sAttachments; }
            set { sAttachments = value; }
        }


        public string SmtpSever
        {
            get { return sSmtpServer; }
            set { sSmtpServer = value; }
        }

        public string From
        {
            get { return sFrom; }
            set { sFrom = value; }
        }

        public string To
        {
            get { return sTo; }
            set { sTo = value; }
        }

        public string Cc
        {
            get { return sCc; }
            set { sCc = value; }
        }

        public string Bcc
        {
            get { return sBcc; }
            set { sBcc = value; }
        }

        public string Subject
        {
            get { return sSubject; }
            set { sSubject = value; }
        }


        public string Body
        {
            get { return sBody; }
            set { sBody = value; }
        }

        public MailPriority MailPriority
        {
            get { return mailPriority; }
            set { mailPriority = value; }
        }

        public MailThread(MailHelper mailHelper)
        {
            this.mailHelper = mailHelper;
            this.sSmtpServer = mailHelper.sSmtpServer;
            this.sFrom = "";
            this.sTo = "";
            this.sCc = "";
            this.sBcc = "";
            this.sSubject = "";
            this.mailPriority = MailPriority.Normal;
            this.sBody = "";
            this.sAttachments = "";
        }

        public void SendMail()
        {
            MailMessage mailMsg = new MailMessage();
            mailMsg.From = new MailAddress(sFrom);
            mailMsg.Subject = sSubject;
            mailMsg.Body = sBody;
            SetMailTo(mailMsg);
            SetMailCc(mailMsg);
            SetMailBcc(mailMsg);
            SetMailAttachments(mailMsg);

            mailMsg.BodyEncoding = Encoding.GetEncoding("UTF-8");
            mailMsg.IsBodyHtml = true;
            SmtpClient sc = new SmtpClient();
            sc.Credentials = new System.Net.NetworkCredential(mailHelper.sUser, mailHelper.sPassword);
            sc.DeliveryMethod = SmtpDeliveryMethod.Network;
            sc.EnableSsl = false;
            sc.Host = sSmtpServer;




            sc.Send(mailMsg);
        }

        public void SetMailAttachments(MailMessage mailMsg)
        {
            if (sAttachments.Trim().Equals("")) { return; }
            string[] sAttachment = sAttachments.Split('|');
            foreach (string sAttach in sAttachment)
            {
                Attachment attachment = new Attachment(sAttach);
                mailMsg.Attachments.Add(attachment);
            }
        }

        public void SetMailTo(MailMessage mailMsg)
        {
            string[] sMailToList = sTo.Split(';');
            foreach (string sMailTo in sMailToList)
            {
                mailMsg.To.Add(sMailTo);
            }
        }

        public void SetMailCc(MailMessage mailMsg)
        {
            if (sCc.Trim().Equals("")) { return; }
            string[] sMailCcList = sCc.Split(';');
            foreach (string sMailCc in sMailCcList)
            {
                mailMsg.CC.Add(sMailCc);
            }
        }

        public void SetMailBcc(MailMessage mailMsg)
        {
            if (sBcc.Trim().Equals("")) { return; }
            string[] sMailBccList = sTo.Split(';');
            foreach (string sMailBcc in sMailBccList)
            {
                mailMsg.Bcc.Add(sMailBcc);
            }
        }
    }
}